package com.xms.schema.service.attribute;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.schema.core.AttributeTypeIds;
import com.xms.schema.entity.Attribute;
import com.xms.schema.extension.AttributeExt;
import com.xms.schema.service.optionset.IOptionSetFinderService;
import com.xms.schema.service.stringmap.IStringMapFinderService;
import com.xms.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * AttributeUnionFinderService
 * </p>
 *
 * @author migo
 * @since 2021/10/21
 */
@CacheConfig(cacheNames = "schema.attribute", keyGenerator = "CacheKeyGenerator")
@Service
public class AttributeUnionFinderService extends AttributeFinderService implements IAttributeUnionFinderService {
    private IStringMapFinderService _stringMapFinderService;
    private IOptionSetFinderService _optionSetFinderService;

    @Autowired
    public AttributeUnionFinderService(IStringMapFinderService stringMapFinderService,
                                       IOptionSetFinderService optionSetFinderService) {
        _stringMapFinderService = stringMapFinderService;
        _optionSetFinderService = optionSetFinderService;
    }

    @Override
    public void wrapUnion(Attribute entity) {
        if (entity == null)
            return;
        if (UUIDUtil.notEmpty(entity.getOptionsetid())) {
            entity.setOptionset(_optionSetFinderService.getById(entity.getOptionsetid(), true));
        } else if (AttributeExt.typeIsPickList(entity) || AttributeExt.typeIsBit(entity)
                || AttributeExt.typeIsState(entity) || AttributeExt.typeIsStatus(entity)) {
            entity.setPicklists(_stringMapFinderService.getByAttributeid(entity.getAttributeid()));
        }
    }

    @Override
    public void wrapUnion(List<Attribute> entities) {
        if (entities == null)
            return;
        entities.forEach(x -> {
            this.wrapUnion(x);
        });
    }

    @Cacheable(condition = "#id != null")
    @Override
    public Attribute getById(UUID id) {
        Attribute entity = super.getById(id);
        if (entity != null)
            this.wrapUnion(entity);
        return entity;
    }

    @Cacheable(condition = "#idList != null && #idList.size() > 0")
    @Override
    public List<Attribute> getByIds(Collection<UUID> idList) {
        List<Attribute> result = super.listByIds(idList);
        if (result != null)
            this.wrapUnion(result);
        return result;
    }

    //@Cacheable()
    @Override
    public Attribute getOne(Wrapper<Attribute> queryWrapper) {
        Attribute entity = super.getOne(queryWrapper);
        if (entity != null)
            this.wrapUnion(entity);
        return entity;
    }

    //@Cacheable()
    @Override
    public List<Attribute> list(Wrapper<Attribute> queryWrapper) {
        List<Attribute> result = super.list(queryWrapper);
        if (result != null)
            this.wrapUnion(result);
        return result;
    }

    @Cacheable()
    @Override
    public List<Attribute> all() {
        List<Attribute> result = super.list();
        if (result != null)
            this.wrapUnion(result);
        return result;
    }

    @Cacheable(condition = "#entityId != null && #name != null")
    @Override
    public Attribute getByName(UUID entityId, String name) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityid, entityId)
                .eq(Attribute::getName, name);
        Attribute entity = super.getOne(queryWrapper);
        if (entity != null)
            this.wrapUnion(entity);
        return entity;
    }

    @Cacheable(condition = "#entityName != null && #name != null")
    @Override
    public Attribute getByName(String entityName, String name) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityname, entityName)
                .eq(Attribute::getName, name);
        Attribute entity = super.getOne(queryWrapper);
        if (entity != null)
            this.wrapUnion(entity);
        return entity;
    }

    @Cacheable(condition = "#entityId != null && #name != null")
    @Override
    public List<Attribute> getByName(UUID entityId, Collection<String> name) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityid, entityId)
                .in(Attribute::getName, name);
        List<Attribute> result = super.list(queryWrapper);
        if (result != null)
            this.wrapUnion(result);
        return result;
    }

    @Cacheable(condition = "#entityName != null && #name != null")
    @Override
    public List<Attribute> getByName(String entityName, Collection<String> name) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityname, entityName)
                .in(Attribute::getName, name);
        List<Attribute> result = super.list(queryWrapper);
        if (result != null)
            this.wrapUnion(result);
        return result;
    }

    @Cacheable(condition = "#entityId != null")
    @Override
    public List<Attribute> getByEntityId(UUID entityId) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityid, entityId);
        List<Attribute> result = super.list(queryWrapper);
        if (result != null)
            this.wrapUnion(result);
        return result;
    }

    @Cacheable(condition = "#entityName != null")
    @Override
    public List<Attribute> getByEntityName(String entityName) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityname, entityName);
        List<Attribute> result = super.list(queryWrapper);
        if (result != null)
            this.wrapUnion(result);
        return result;
    }

    @Cacheable(condition = "#entityId != null")
    @Override
    public List<Attribute> getBySummaryEntityId(UUID entityId) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getSummaryentityid, entityId);
        List<Attribute> result = super.list(queryWrapper);
        if (result != null)
            this.wrapUnion(result);
        return result;
    }

    @Cacheable(condition = "#entityId != null")
    @Override
    public Attribute getPrimaryKey(UUID entityId) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityid, entityId)
                .eq(Attribute::getAttributetypename, AttributeTypeIds.PRIMARYKEY);
        Attribute entity = super.getOne(queryWrapper);
        if (entity != null)
            this.wrapUnion(entity);
        return entity;
    }

    @Cacheable(condition = "#entityName != null")
    @Override
    public Attribute getPrimaryKey(String entityName) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityname, entityName)
                .eq(Attribute::getAttributetypename, AttributeTypeIds.PRIMARYKEY);
        Attribute entity = super.getOne(queryWrapper);
        if (entity != null)
            this.wrapUnion(entity);
        return entity;
    }

    @Cacheable(condition = "#entityId != null")
    @Override
    public Attribute getPrimaryField(UUID entityId) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityid, entityId)
                .eq(Attribute::getIsprimaryfield, true);
        Attribute entity = super.getOne(queryWrapper);
        if (entity != null)
            this.wrapUnion(entity);
        return entity;
    }

    @Cacheable(condition = "#entityName != null")
    @Override
    public Attribute getPrimaryField(String entityName) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityname, entityName)
                .eq(Attribute::getIsprimaryfield, true);
        Attribute entity = super.getOne(queryWrapper);
        if (entity != null)
            this.wrapUnion(entity);
        return entity;
    }

    @Cacheable()
    @Override
    public List<Attribute> getAuthorizations() {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getAuthorizationenabled, true);
        List<Attribute> result = super.list(queryWrapper);
        if (result != null)
            this.wrapUnion(result);
        return result;
    }
}
